import java.util.List;

public class SingleLinkedList {

    private ListNode head = null;       //头结点
    private ListNode lastNode = null;   //尾结点
    private int size = 0;       //当前单链表的长度
    static class ListNode {
        int val;
        ListNode next;

        public ListNode() {}
        public ListNode(int val) {
            this.val = val;
        }
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        // 若单链表中没有结点，则将 lastNode结点 指向 node结点
        if(head == null) {
            lastNode = node;
            size++;
            return;
        }
        node.next = head;
        head = node;
        size++;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        // 判断单链表中是否有结点
        if(head == null) {
            head = node;
            lastNode = node;
            size++;
            return;
        }
        lastNode.next = node;
        lastNode = node;
        size++;
    }


    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if(index < 0 || index > size) {
            return false;
        }
        if(index == 0) {
            addFirst(data);
            return true;
        }
        if(index == size) {
            addLast(data);
            return true;
        }

        ListNode cur = head;
        ListNode prev = head;
        for (int i = 0; i < index; i++) {
            prev = cur;
            cur = cur.next;
        }
        ListNode node = new ListNode(data);
        node.next = cur;
        prev.next = node;
        size++;
        return true;
    }


    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) return;
        // 判断是否为头结点
        if(head.val == key) {
            head = head.next;
            size--;
            return;
        }

        ListNode cur = head;
        ListNode prev = head;
        while(cur != null) {
            if(cur.val == key) {
                break;
            }
            prev = cur;
            cur = cur.next;
        }
        if(cur == null) return;

        // 判断 cur 是否为尾结点
        if(cur == lastNode) {
            lastNode = prev;
        }
        prev.next = cur.next;
        size--;
    }


    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) return;
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == lastNode) {
                    lastNode = prev;
                }
                prev.next = cur.next;
                cur = cur.next;
                size--;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        // 判断头结点的值是否为 key
        if(head.val == key) {
            head = head.next;
            size--;
        }
    }
    //得到单链表的长度
    public int size() {
        return size;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        head = null;
        lastNode = null;
    }
}
